package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lh.commonutils.JwtUtils;

import com.lh.commonutils.MD5;
import com.lh.entity.UcenterMember;
import com.lh.dao.UcenterMemberMapper;
import com.lh.entity.vo.RegisterVo;
import com.lh.service.IUcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.servicebase.exceptionhandler.MyException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lh
 * @since 2021-06-09
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements IUcenterMemberService {


    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Resource
    private UcenterMemberMapper ucenterMemberMapper;
//    登录的方法
    @Override
    public String login(UcenterMember member) {
//        获取手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

//        手机号和密码的非空判断
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw  new MyException(20001,"登录失败");
        }
//        判断会员是否存在
        UcenterMember member1 = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if(member1 == null){
            throw  new MyException(20001,"登录失败");
        }
//        判断密码
        if(!MD5.encrypt(password).equals(member1.getPassword())){
            throw  new MyException(20001,"登录失败");
        }
//        判断是否禁用
        if(member1.getIsDeleted()){
            throw  new MyException(20001,"登录失败");
        }
//        使用jwt生成token字符串
        String jwtToken = JwtUtils.getJwtToken(member1.getId(), member1.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        if (StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new MyException(20001, "error");
        }

        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobleCode)) {
            throw new MyException(20001, "error");
        }

        //查询数据库中是否存在相同的手机号码
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (count.intValue() > 0) {
            throw new MyException(20001, "error");
        }

        //添加注册信息到数据库
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setIsDeleted(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        this.save(member);

    }

    /**
     * 修改用户信息
     *
     * @param ucenterMember
     */
    @Override
    public void updateUcenter(UcenterMember ucenterMember) {
        if (ucenterMember.getPassword() !=null){
            ucenterMember.setPassword(MD5.encrypt(ucenterMember.getPassword()));
            ucenterMemberMapper.updateById(ucenterMember);
        }else {
            ucenterMemberMapper.updateById(ucenterMember);
        }
    }

    /**
     * //查询某一天注册人数
     *
     * @param day
     * @return
     */
    @Override
    public Integer countRegister(String day) {

        Integer count = ucenterMemberMapper.countRegister(day);
        return count;
    }
}
